1   package com.iluwatar;
2   
3   import java.awt.Color;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   
7   import javax.swing.JButton;
8   import javax.swing.JFrame;
9   import javax.swing.JLabel;
10  import javax.swing.JOptionPane;
11  import javax.swing.JPanel;
12  import javax.swing.JScrollPane;
13  import javax.swing.JTextArea;
14  import javax.swing.JTextField;
15  
16  /**
17   * This class is the GUI implementation of the View component In the
18   * Model-View-Presenter pattern.
19   */
20  public class FileSelectorJFrame extends JFrame implements FileSelectorView,
21  		ActionListener {
22  
23  	/**
24  	 * Default serial version ID.
25  	 */
26  	private static final long serialVersionUID = 1L;
27  
28  	/**
29  	 * The "OK" button for loading the file.
30  	 */
31  	private JButton OK;
32  
33  	/**
34  	 * The cancel button.
35  	 */
36  	private JButton cancel;
37  
38  	/**
39  	 * The information label.
40  	 */
41  	private JLabel info;
42  
43  	/**
44  	 * The contents label.
45  	 */
46  	private JLabel contents;
47  
48  	/**
49  	 * The text field for giving the name of the file that we want to open.
50  	 */
51  	private JTextField input;
52  
53  	/**
54  	 * A text area that will keep the contents of the file opened.
55  	 */
56  	private JTextArea area;
57  
58  	/**
59  	 * The panel that will hold our widgets.
60  	 */
61  	private JPanel panel;
62  
63  	/**
64  	 * The Presenter component that the frame will interact with
65  	 */
66  	private FileSelectorPresenter presenter;
67  
68  	/**
69  	 * The name of the file that we want to read it's contents.
70  	 */
71  	private String fileName;
72  
73  	/**
74  	 * Constructor.
75  	 */
76  	public FileSelectorJFrame() {
77  		super("File Loader");
78  		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
79  		this.setLayout(null);
80  		this.setBounds(100, 100, 500, 200);
81  
82  		/*
83  		 * Add the panel.
84  		 */
85  		this.panel = new JPanel();
86  		panel.setLayout(null);
87  		this.add(panel);
88  		panel.setBounds(0, 0, 500, 200);
89  		panel.setBackground(Color.LIGHT_GRAY);
90  
91  		/*
92  		 * Add the info label.
93  		 */
94  		this.info = new JLabel("File Name :");
95  		this.panel.add(info);
96  		info.setBounds(30, 10, 100, 30);
97  
98  		/*
99  		 * Add the contents label.
100 		 */
101 		this.contents = new JLabel("File contents :");
102 		this.panel.add(contents);
103 		this.contents.setBounds(30, 100, 120, 30);
104 
105 		/*
106 		 * Add the text field.
107 		 */
108 		this.input = new JTextField(100);
109 		this.panel.add(input);
110 		this.input.setBounds(150, 15, 200, 20);
111 
112 		/*
113 		 * Add the text area.
114 		 */
115 		this.area = new JTextArea(100, 100);
116 		JScrollPane pane = new JScrollPane(area);
117 		pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
118 		pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
119 		this.panel.add(pane);
120 		this.area.setEditable(false);
121 		pane.setBounds(150, 100, 250, 80);
122 
123 		/*
124 		 * Add the OK button.
125 		 */
126 		this.OK = new JButton("OK");
127 		this.panel.add(OK);
128 		this.OK.setBounds(250, 50, 100, 25);
129 		this.OK.addActionListener(this);
130 
131 		/*
132 		 * Add the cancel button.
133 		 */
134 		this.cancel = new JButton("Cancel");
135 		this.panel.add(this.cancel);
136 		this.cancel.setBounds(380, 50, 100, 25);
137 		this.cancel.addActionListener(this);
138 
139 		this.presenter = null;
140 		this.fileName = null;
141 	}
142 
143 	@Override
144 	public void actionPerformed(ActionEvent e) {
145 		if (e.getSource() == this.OK) {
146 			this.fileName = this.input.getText();
147 			presenter.fileNameChanged();
148 			presenter.confirmed();
149 		}
150 
151 		else if (e.getSource() == this.cancel) {
152 			presenter.cancelled();
153 		}
154 	}
155 
156 	@Override
157 	public void open() {
158 		this.setVisible(true);
159 	}
160 
161 	@Override
162 	public void close() {
163 		this.dispose();
164 	}
165 
166 	@Override
167 	public boolean isOpened() {
168 		return this.isVisible();
169 	}
170 
171 	@Override
172 	public void setPresenter(FileSelectorPresenter presenter) {
173 		this.presenter = presenter;
174 	}
175 
176 	@Override
177 	public FileSelectorPresenter getPresenter() {
178 		return this.presenter;
179 	}
180 
181 	@Override
182 	public void setFileName(String name) {
183 		this.fileName = name;
184 	}
185 
186 	@Override
187 	public String getFileName() {
188 		return this.fileName;
189 	}
190 
191 	@Override
192 	public void showMessage(String message) {
193 		JOptionPane.showMessageDialog(null, message);
194 	}
195 
196 	@Override
197 	public void displayData(String data) {
198 		this.area.setText(data);
199 	}
200 }